Skip to content

fix: more cached type hints#6161

Merged
masenf merged 3 commits intoreflex-dev:mainfrom
benedikt-bartscher:more-cached-type-hints
Mar 12, 2026
Merged

fix: more cached type hints#6161
masenf merged 3 commits intoreflex-dev:mainfrom
benedikt-bartscher:more-cached-type-hints

Conversation

@benedikt-bartscher
Copy link
Copy Markdown
Contributor

No description provided.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 6, 2026

Merging this PR will not alter performance

βœ… 8 untouched benchmarks


Comparing benedikt-bartscher:more-cached-type-hints (fb3f51b) with main (3c11451)

Open in CodSpeed

@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review March 6, 2026 21:55
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 6, 2026

Greptile Summary

This PR is a small, focused performance improvement that extends the "cached type hints" pattern to the event-handler processing path in BaseState. It replaces the uncached typing.get_type_hints(handler.fn) call with types.get_type_hints(handler.fn) β€” the @lru_cache-wrapped wrapper in reflex.utils.types β€” so that repeated lookups of the same function's type annotations hit the cache rather than re-evaluating annotations on every event dispatch. The standalone import typing is removed as it is no longer referenced.

Changes:

  • typing.get_type_hints(handler.fn) β†’ types.get_type_hints(handler.fn) (cached), avoiding redundant annotation evaluation on each event dispatch.
  • import typing removed β€” no remaining typing.X usages exist in the file.
  • The directly-imported get_type_hints (line 32, from typing import get_type_hints) is retained because it is still used at line 814 (with a localns= argument that the cached wrapper does not support) and at line 2535.

Why safe: The change is minimal and well-scoped: it replaces one uncached typing.get_type_hints call with the project's own @lru_cache-decorated wrapper. The cache key (handler.fn) is a stable, hashable function object. No behavioural change is introduced β€” only a caching improvement on every event dispatch.

Confidence Score: 5/5

  • This PR is safe to merge β€” it is a two-line focused change with no logic alteration, only a switch to the already-established cached wrapper.
  • The change is minimal and well-scoped: it replaces one uncached typing.get_type_hints call with the project's own @lru_cache-decorated wrapper, and correctly removes the now-unused import typing. The cache key (handler.fn) is a stable, hashable function object. No behavioural change is introduced β€” only a caching improvement on every event dispatch.
  • No files require special attention.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Event dispatched to BaseState._process_event] --> B[Build functools.partial fn from handler.fn]
    B --> C{types.get_type_hints\nhandler.fn}
    C -- Cache hit --> D[Return cached type hints dict]
    C -- Cache miss --> E[typing.get_type_hints_og\nevaluates annotations]
    E --> F[Store result in lru_cache]
    F --> D
    D --> G[Iterate payload args]
    G --> H{Type hint found\nfor arg?}
    H -- No / Any --> I[Skip coercion]
    H -- Yes --> J[Coerce value to hinted type]
    I --> K[Process next arg]
    J --> K
Loading

Last reviewed commit: 39da464

@masenf masenf merged commit 1b39a5a into reflex-dev:main Mar 12, 2026
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants